﻿<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="org.jasig.cas.client.authentication.AttributePrincipal,
				 com.hatic.login.PmLoginUtil,java.util.Map" %>
<%
	String casUserId = "";//集成系统中的用户ID
	String contextpath = request.getContextPath();
	
	String myname = (String) request.getSession().getAttribute("myname");

	AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal(); 
	if (principal != null) {
		Map attributes = principal.getAttributes();
		Object valuesObject = attributes.get("attribute");
		
		if(null != valuesObject){
			String values = valuesObject.toString().replaceAll("\n", ",").replaceAll(" ", "");
			String[] value = values.substring(1, values.length()-1).split(",");
			if (value.length > 1) casUserId = value[4];//集成系统中的用户ID
		}
	}
	//根据代办传递的assign_id    
	String pmassign_id = (String) request.getParameter("assign_id");
	//根据集成用户ID找到PM项目中的用户编码
	String pmUserCode = (String) request.getParameter("user_code");
	if (pmUserCode == null || pmUserCode.length() == 0) {
		if (casUserId.length() > 0) {
			pmUserCode = PmLoginUtil.queryUserCode(casUserId);
		}
	}
%>
<body>
<form action="<%=contextpath%>/commonAction.do" id="frmLogin" name="frmLogin" method="post" target="frmLoginIframe" style="display:none">
<input type="hidden" id="funid" name="funid" value="login">
<input type="hidden" id="eventcode" name="eventcode" value="login">
<input type="hidden" id="pagetype" name="pagetype" value="login">
<input type="hidden" id="user_code" name="user_code" value="<%=pmUserCode%>">
<input type="hidden" id="url_assign" name="url_assign" value="<%=pmassign_id%>">
<input type="hidden" id="iscas" name="iscas" value="1">
</form>
<iframe style="display:none" id="frmLoginIframe" name="frmLoginIframe"></iframe>
</body>
<SCRIPT LANGUAGE="JavaScript">
<!--
	//打开新页面
	var openIndex = function() {
		if(document.getElementById('url_assign').value!=null){
			var url = './cas_main.jsp?assign_id='+document.getElementById('url_assign').value;
		}
		else{
			var url = './cas_main.jsp';
		}
		
		var mainwin = window.open(url, '_self',//'jxstar_main_window',
			'resizable=yes,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no');
		if(null == mainwin){
			alert("当前浏览器不允许弹出窗口！");
		}else{
			mainwin.focus();
			mainwin.moveTo(0,0);
			mainwin.resizeTo(screen.availWidth,screen.availHeight);
		}
		/*
		window.opener=null; 
		window.open("","_self"); 
		window.close(); */
	}
	
	//登录系统后的回调方法
	var frmLogin = document.getElementById('frmLoginIframe');
	frmLogin.onload = function() {
		var text = "window.loginResult = " + frmLogin.contentWindow.document.body.innerHTML + ";";
		if (window.execScript) {
		   window.execScript(text);
		} else {
		   window.eval(text);
		}
		var result = window.loginResult;
		if (result && result.success == false) {
			alert(result.message);
		}
		window.loginResult = null;
		
		openIndex();
	};
	
	var pmUserCode = '<%=pmUserCode%>';
	if (pmUserCode.length == 0) {
		alert('没有找到在项目管理系统中的对应用户信息！');
	} else {
		//直接登录系统
		var frm = document.getElementById('frmLogin');
		frm.submit();
	}
//-->
</SCRIPT>
</html>